package com.ziyun.pop.web.modules.goodsCraftPrice.controller;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.utility.JsonUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.goodsCraftPrice.dto.GoodsCraftPriceDto;
import com.ziyun.pop.modules.goodsCraftPrice.dto.GoodsTemplateOptionDto;
import com.ziyun.pop.modules.goodsCraftPrice.entity.GoodsCraftPriceEntity;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 标品工艺价格表 全部采用接口调用方式 调用ERP API
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-09-10 10:43:12
 */
@RestController
@RequestMapping("goodsCraftPrice/goodscraftprice")
public class GoodsCraftPriceController {

    @Value("${erpApi}")
    private String erpApi ;

    /**
     * 获取基础商品工艺价格
     */
    @RequestMapping("/getGoodsCraftPriceList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftPriceList(){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsCraftPriceList";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 获取基础商品模板工艺价格
     */
    @RequestMapping("/getGoodsTemplateCraftPriceList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateCraftPriceList(){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsTemplateCraftPriceList";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 获取基础商品模板
     */
    @RequestMapping("/getGoodsTemplateList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateList(Integer attributeId){

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("attributeId", attributeId);

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsTemplateList";
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 获取商品模板 关联的选项
     * @param goodsTemplateOptionDto
     * @return
     */
    @RequestMapping("/getGoodsTemplateOptionList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateOptionList(@RequestBody GoodsTemplateOptionDto goodsTemplateOptionDto){

        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();
        goodsTemplateOptionDto.setSellerId(sellerId);

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsTemplateOptionList";
        String response =  HttpUtils.doPost(url,goodsTemplateOptionDto);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 获取基础商品工艺
     * @return
     */
    @RequestMapping("/getGoodsCraftList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftList(){

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsCraftList";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        String response =  HttpUtils.doPost(url,paramBodyMap);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 获取基础商品工艺选项
     * @param attributeId
     * @return
     */
    @RequestMapping("/getGoodsCraftOptionList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsCraftOptionList(Integer attributeId){
        // 获取当前用户的商户id
        Long sellerId =  UserUtils.getCurentUser().getSellerId();

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsCraftOptionList";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("attributeId", attributeId);
        String response =  HttpUtils.doPost(url,paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 保存基础商品工艺价格
     * @param goodsCraftPriceDto
     * @return
     */
    @RequestMapping("/save")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:save")
    public R save(@Valid @RequestBody GoodsCraftPriceDto goodsCraftPriceDto){
        // 获取当前登录人
        SysUserEntity sysUserEntity = UserUtils.getCurentUser();
        String userName = sysUserEntity.getUsername();
        Long sellerId = sysUserEntity.getSellerId();

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/save";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("userName", userName);

        paramBodyMap.put("craftPriceList", JsonUtils.object2String(goodsCraftPriceDto.getCraftPriceList()));


        String response =  HttpUtils.doPost(url,paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }


    /**
     * 根据工艺删除
     * @param attributeId
     * @return
     */
    @RequestMapping("/deleteByAttributeId")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:delete")
    public R deleteByAttributeId(Integer attributeId){
        Long sellerId = UserUtils.getCurentUser().getSellerId();

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/deleteByAttributeId";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("sellerId", sellerId);
        paramBodyMap.put("attributeId", attributeId);
        String response =  HttpUtils.doPost(url,paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 根据工艺陪价删除
     * @param ids
     * @return
     */
    @RequestMapping("/deleteByIds")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:delete")
    public R deleteByIds(@RequestBody List<Integer> ids){

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/deleteByIds";

        Map<String, Object> paramBodyMap = new HashMap<String, Object>();
        paramBodyMap.put("ids", JsonUtils.object2String(ids));
        String response =  HttpUtils.doPost(url,paramBodyMap);

        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }


    /**
     * 获取基础商品模板工艺价格 弹窗用
     */
    @RequestMapping("/getGoodsTemplateCraftPriceWindowList")
//    @RequiresPermissions("goodsCraftPrice:goodscraftprice:info")
    public R getGoodsTemplateCraftPriceWindowList(@RequestParam Integer templateId, @RequestParam String attributeIds){
        //商户id
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        Map<String,Object> params = new HashMap<>();
        params.put("sellerId", sellerId);
        params.put("templateId", templateId);
        params.put("attributeIds", attributeIds);

        String url = erpApi + "/goodsCraftPrice/goodscraftprice/getGoodsTemplateCraftPriceWindowList";

        String response =  HttpUtils.doPost(url,params);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

}
